package com.ym.controller.soso;

import com.github.pagehelper.PageInfo;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.soso.domain.entity.PensionInfoEntity;
import com.ym.soso.domain.param.PensionInfoQueryParam;
import com.ym.soso.domain.param.PensionInfoUpdateParam;
import com.ym.soso.domain.param.PublishParam;
import com.ym.soso.domain.vo.PensionInfoListPcVO;
import com.ym.soso.service.IPensionInfoSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 养老Controller
 * 
 * @author ym
 * @date 2020-04-02
 */
@Api(tags = "PC-养老")
@RestController("PensionInfoPcController")
@Validated
@RequestMapping("/pc/pension")
public class PensionInfoController extends BaseController {

    @Autowired
    private IPensionInfoSV pensionInfoSV;

    @ApiOperation("查询养老列表")
    @Log(dataBase = false, printBody = false, printQuery = true)
    @GetMapping("/listPage")
    public APIResponse<PageInfo<PensionInfoListPcVO>> list(@Valid PensionInfoQueryParam queryParam) {
        PageDomain.startPage();
        List<PensionInfoListPcVO> list = pensionInfoSV.selectListPC(queryParam);
        return successPage(list);
    }

    @ApiOperation("获取养老详细信息")
    @Log(dataBase = false, printBody = false, printQuery = true)
    @GetMapping(value = "/info")
    public APIResponse<PensionInfoEntity> info(@RequestParam Long id) {
        PensionInfoEntity data = pensionInfoSV.selectById(id);
        return successData(data);
    }

    @ApiOperation("新增养老")
    @Log(dataBase = true, printBody = true, printQuery = false)
    @PostMapping("/add")
    public APIResponse update(@RequestBody @Valid PensionInfoUpdateParam updateParam) {
        Long id = pensionInfoSV.insertPC(updateParam);
        return successData(id);
    }

    @Log
    @ApiOperation("PC-养老信息修改")
    @PostMapping("/update")
    @ResponseBody
    public APIResponse updatePensionInfo(@RequestBody @Valid PensionInfoUpdateParam updateParam)
    {
        pensionInfoSV.updateByIdPC(updateParam);
        return successData(updateParam.getId());
    }

    @ApiOperation("删除养老")
    @Log(dataBase = true, printBody = true, printQuery = false)
    @PostMapping("/delete")
    public APIResponse delete(@RequestBody @Valid @NotNull Long[] ids) {
        int result =  pensionInfoSV.deleteByIds(ids);

        return successData(result);
    }

    @ApiOperation("发布")
    @PostMapping(value = "/release", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public APIResponse release(
            @RequestBody @Valid PublishParam publishParams
    ) {
        pensionInfoSV.release(publishParams);
        return success();
    }

}
